1474B - Different Divisors - CodeForces Solution


binary search constructive algorithms greedy math number theory *1000

Please click on ads to support us..

Python Code:

def Sieve(n):
    prime = [True for i in range(n + 1)]
    p = 2
    while (p * p <= n):
        if (prime[p] == True):
            for i in range(p ** 2, n + 1, p):
                prime[i] = False
        p += 1
    prime[0]= False
    prime[1]= False
    return(prime)

prime=Sieve(100000)
for _ in range(int(input())):
    n=int(input())
    a=n+1
    for i in range(a,len(prime)):
        if(prime[i]):
            p=i
            break
    for i in range(p+n,len(prime)):
        if(prime[i]):
            q=i
            break
    print(p*q)

C++ Code:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll arr[100000];
void sieve(vector<ll>&v){
    arr[0]=0;
    arr[1]=0;
    fill(arr,arr+100000,1);
    for(ll i=2;i*i<=100000;i++){
        if(arr[i]==1){
            v.push_back(i);
            for(ll j=i*i;j<=100000;j+=i){
                arr[j]=0;
            }
        }
    }
}
int main() {
 vector<ll>v;
 sieve(v);
    ll t;cin>>t;
    while(t--){
       ll d;cin>>d;
       ll p1=1;
       ll p2=1;
       ll i=d+1;
       while(arr[i]!=1){
           i++;
       }
       p1=i;
       i=p1+d;
        while(arr[i]!=1){
           i++;
       }
       p2=i;
       ll ans=p1*p2;
       cout<<ans<<"\n";
}
}


Comments

Submit
0 Comments
More Questions

1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon